<?
//<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
session_start();
require ("inc.php");
$db_admin    = &new db($scfg['db']['admin']);
$rows  = 10;
$page  = (int)($_REQUEST['page']?$_REQUEST['page']:1);
admin_core::check("sys@manage");
/* 以下是程序部份 */

$table = $admin['table']['name'];
if($_GET['Pact'] || $_POST['Pact'])
{
	$Pact = $_POST['Pact']?$_POST['Pact']:$_GET['Pact'];
	//添加
	if($Pact == 'add')
	{
		$bind = array(
			"master"=>$_POST["master"],
			"nickname"=>$_POST["nickname"],
			"name"=>$_POST["name"],
			"branch"=>$_POST["branch"],
			"area"=>$_POST["area"],
			"tel"=>$_POST["tel"],
			"mobile"=>$_POST["mobile"],
			"email"=>$_POST["email"],
			"password"=>admin_core::mixPass($_POST["password"]),
			"keyField"=>serialize($_POST["keyField"]),
			"content"=>$_POST["content"],
		);
		$bind[$admin['table']['field']['higherUp']]     = admin_core::getMaster();
		if(admin_core::isSuper())
		{
			$bind[$admin['table']['field']['superMaster']]  = $_POST["superMaster"];
		}
		$rs = $db_admin->rsArray("select * from `{$table}` where `{$admin['table']['field']['master']}` = ? ",$_POST["master"]);
		if($rs)
		{
			echo g::msg("提示:帐号“{$_POST['master']}”以存在！请换一个后重试!");
		}else
		{
			$id = $db_admin->insert($table,$bind);
			if($id)
			{
				echo g::msg("成功:帐号“{$_POST['master']}”添加成功！");
				g::gowin("?act=modify&id=".$id);
			}else
			{
				echo g::msg("失败:系统忙！请重试!");
			}
		}
	//更新
	}elseif($Pact == 'modify' && $_POST['id'])
	{
		$bind = array(
			"nickname"=>$_POST["nickname"],
			"name"=>$_POST["name"],
			"branch"=>$_POST["branch"],
			"area"=>$_POST["area"],
			"tel"=>$_POST["tel"],
			"mobile"=>$_POST["mobile"],
			"email"=>$_POST["email"],
			"keyField"=>serialize($_POST["keyField"]),
			"content"=>$_POST["content"],
		);
		$where = array(
					'where' => " `id` = :id ",
					'bind'  => array('id'=>$_POST["id"])
				 );
		if(admin_core::isSuper() && admin_core::getMaster() != $_POST["master"])
		{
			$_POST["higherUp"]
				&& $bind[$admin['table']['field']['higherUp']] 	= $_POST["higherUp"];
			$bind[$admin['table']['field']['superMaster']]  = $_POST["superMaster"];
		}else
		{
			$where['where'] .= " and `{$admin['table']['field']['higherUp']}` = :{$admin['table']['field']['higherUp']} ";
			$where['bind'][$admin['table']['field']['higherUp']] = admin_core::getMaster();
		}
		$db_admin->update($table,$bind,$where["where"],$where["bind"]);
		//echo $db_admin->getsql();
		//exit();
		if($_POST["password"] != "**********")
		{
			$bind = array("password"=>admin_core::mixPass($_POST["password"]));
			$db_admin->update($table,$bind," `id` = ? ",$_POST["id"]);
		}
		admin_core::cleanPopedom($_POST['master']);
		echo g::msg("成功:帐号“{$_POST['master']}”更新成功！");
		g::gowin("?act=modify&id=".$_POST["id"]);
	}elseif($Pact == 'del' && $_GET['id'])
	{
		$where = array(
			'where' => " where id = :id ",
			'bind'  => array('id' => $_GET['id'])
		);
		$rs    = $db_admin->rsArray("select * from {$table} {$where['where']}",$where['bind']);	
		if($rs['master'] == admin_core::getMaster())
		{
			echo g::msg("提示:不能删除自己！");
		}elseif($rs['superMaster'] && !admin_core::isSuper())
		{
			echo g::msg("提示:帐号“{$rs['master']}”为超级管里员不能删除！");
		}else
		{
			$rs = $db_admin->delete($table," id = ? ",$_GET['id']);
			if($rs)
			{
				echo g::msg("删除成功:帐号“{$rs['master']}”删除成功！");
			}else
			{
				echo g::msg("失败:系统忙，请稍后再试。");
			}
		}
		g::gowin("?act=manage");
	}
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link  href="static/admincp.css" rel="stylesheet" type="text/css" id="css">
<script src="Script/lib.js" 	type="text/javascript"></script>
<script src="Script/lib.ext.js" type="text/javascript"></script>
<script src="Script/lib.ext.oAdmin.js" type="text/javascript"></script>
<script type="text/javascript">
if(typeof XypForm == "undefined")
{
	_.r('<script src="static/xypform/xypfrom.js" type="text/javascript"></sc'+'ript>');
	_.r('<link  href="static/xypform/xypform.css" rel="stylesheet" type="text/css">');
}
var collapsed = $.cookie('DTh_collapse');
function collapse_change(menucount)
{
	if(_.e('menu_' + menucount).style.display == 'none') 
	{
		_.e('menu_' + menucount).style.display = '';collapsed = collapsed.replace('[' + menucount + ']' , '');
		_.e('menuimg_' + menucount).src = 'static/img/menu_reduce.gif';
	} else 
	{
		_.e('menu_' + menucount).style.display = 'none';collapsed += '[' + menucount + ']';
		_.e('menuimg_' + menucount).src = 'static/img/menu_add.gif';
	}
	$.cookie('DTh_collapse', collapsed, 2592000);
}
</script>
</head>
<body leftmargin="10" topmargin="10">
<? 
if($_GET['act'] == 'add' || ($_GET['act'] == 'modify' && $_GET['id']))
{
	//新加
	if($_GET['act'] == 'add')
	{
		function popedom()
		{
			$popedom 	= admin_core::getPopedomGroup();
			foreach($popedom as $k=>$v){?>
				<div style="border:#333333 dotted 1px; margin:5px 0 5px; padding:3px;">
					<b>&nbsp;<?=$v['name']?></b>
					<? foreach($v["sub"] as $kk=>$vv){?>
					<div style="display:block; width:100px; height:20px; float:left;">
						<label for="keyField_<?=$k."_".$kk?>">
							<input id="keyField_<?=$k."_".$kk?>" <?=($vv['key']?'':'checked="checked" disabled="disabled"')?> class="checkbox" name="keyField[<?=$k?>][]" type="checkbox" value="<?=$kk?>" /><?=$vv['name']?>
						</label>
					</div>                    
					<? }?>
					<div style="clear:both;"></div>  
				</div>      
			 <? }
		}
		$rs   	=  array("higherUp" => admin_core::getMaster());
		$Pact 	=  'add';
		$Htitle = '添加管理人员';
	//更改
	}elseif($_GET['act'] == 'modify')
	{
		$where = array(
			'where' => " where id = :id ",
			'bind'  => array('id' => $_GET['id'])
		);
		$rs    = $db_admin->rsArray("select * from {$table} {$where['where']}",$where['bind']);	
		function popedom()
		{
			global $admin,$rs;
			$popedomP 	= admin_core::getPopedomGroup();
			$popedom 	= admin_core::getPopedomGroup($rs['master']);
			foreach($admin['purview'] as $k=>$v){?>
				<div style="border:#333333 dotted 1px; margin:5px 0 5px; padding:3px;">
					<b>&nbsp;<?=$v['name']?></b>
					<? foreach($v["sub"] as $kk=>$vv){?>
					<div style="display:block; width:100px; height:20px; float:left;">
						<label for="keyField_<?=$k."_".$kk?>">
                        	<?=(empty($popedomP[$k]["sub"][$kk]) && $popedom[$k]["sub"][$kk]?'<input   type="hidden" name="keyField['.$k.'][]" value="'.$kk.'">':'')?>
							<input  id="keyField_<?=$k."_".$kk?>" <?=($vv['key']?(($popedom[$k]["sub"][$kk]?' checked="checked" ':'').($popedomP[$k]["sub"][$kk]?'':' disabled="disabled" ')):'checked="checked" disabled="disabled"')?> class="checkbox" name="keyField[<?=$k?>][]" type="checkbox" value="<?=$kk?>" /><?=$vv['name']?>
						</label>
					</div>                    
					<? }?>
					<div style="clear:both;"></div>  
				</div>      
			 <? }
		}
		$Pact 			= 'modify';
		$Pid			= '<input type="hidden"  name="id" value="'.$rs["id"].'">';
		$Hpass 			= '<span class="calendar_today">注:此项不设定，将不会更改现有密码！</span>';
		$Htitle 		= "更新<b>{$rs['master']}</b>({$rs['name']})";
		$rs['password'] = $rs['password2'] =  '**********';
	}
?>
<table width="100%" border="0" cellpadding="2" cellspacing="6">
  <tr>
    <td>
      <form method="post" name="settings" id="settings" onsubmit="return XypForm(this)">
        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
          <tr class="header">
            <td colspan="2">人员管理 - <?=$Htitle?></td>
          </tr>
          <tbody>
            <tr>
              <td width="40%" class="altbg1" >
                <b>添加帐号:</b><br />
                <span class="smalltxt">管理人员，登录时用的ID</span>
              </td>
              <td class="altbg2">
              	<input type="text" require="1" minlen="4"  maxlen="20"  dataType="alphanum" <?=($rs["master"]?' readonly="readonly" value="'.$rs["master"].'" ':'')?>  size="50" name="master">
              </td>
            </tr>
            <tr>
              <td width="40%" class="altbg1" >
                <b>管理人员昵称:</b><br />
                <span class="smalltxt">管理人员昵称，或英文名。如:cyberty </span>
              </td>
              <td class="altbg2">
              	<input type="text" value="<?=$rs['nickname']?>" size="50" name="nickname">
              </td>
            </tr>
            <tr>
              <td width="40%" class="altbg1" ><b>真实姓名:</b><br />
                <span class="smalltxt">真实姓名，最好填上吧！</span></td>
              <td class="altbg2"><input  type="text" require="1" minlen="4"  maxlen="10"  size="50" name="name" value="<?=$rs['name']?>" >
              </td>
            </tr>
            <tr>
              <td width="40%" class="altbg1" ><b>所在部门:</b><br />
                <span class="smalltxt">所在部门，请填清楚最好把子部门也写上。如：产品部-推广部</span></td>
              <td class="altbg2"><input type="text" require="1" size="50" name="branch" value="<?=$rs['branch']?>" >
              </td>
            </tr>
            <tr>
              <td width="40%" class="altbg1" ><b>所在地区:</b><br />
                <span class="smalltxt">所在地区，工作所在地。如：广州 北京</span></td>
              <td class="altbg2"><input type="text" require="1" size="50" name="area" value="<?=$rs['area']?>" >
              </td>
            </tr>
            <tr>
              <td width="40%" class="altbg1" ><b>联系电话:</b><br />
                <span class="smalltxt">联系电话，如果可以请填上你的可联系到你的座机，办公室分机，用逗号分开 如:1348022576X,020-8552454X,020-85533243-8025</span></td>
              <td class="altbg2"><input type="text" require="1" size="50" name="tel" value="<?=$rs['tel']?>" ></td>
            </tr>
            <tr>
              <td width="40%" class="altbg1" ><b>移动电话:</b><br />
                <span class="smalltxt">移动电话，如果可以请填上你的随时可联系到你的移动电话</span></td>
              <td class="altbg2"><input type="text" size="50" require="1"  dataType="nums" name="mobile" value="<?=$rs['mobile']?>" ></td>
            </tr>
            <tr>
              <td width="40%" class="altbg1" ><b>EMAIL:</b><br />
                <span class="smalltxt">EMAIL，公司的邮箱地址</span></td>
              <td class="altbg2"><input type="text" size="50" dataType="email" require="1" name="email" value="<?=$rs['email']?>" ></td>
            </tr>
            <tr>
              <td width="40%" class="altbg1" ><b>设定密码:</b><br />
                <span class="smalltxt">设定密码，6-16位。请注意密码的安全
                </span></td>
              <td class="altbg2"><input type="password" require="1"  minlen="6"  maxlen="16" size="50" name="password"  id="password" value="<?=$rs['password']?>"><br />
                <?=$Hpass?></td>
            </tr>
            <tr>
              <td width="40%" class="altbg1" ><b>确认密码:</b><br />
                <span class="smalltxt">确认密码，确认一次</span></td>
              <td class="altbg2"><input type="password" require="1" again="password" size="50" name="password2" id="password2" value="<?=$rs['password2']?>"></td>
            </tr>
            <tr>
              <td width="40%" class="altbg1" ><b>设定权限:</b><br />
                <span class="smalltxt">设定权限，请设定适用的权限，原则上是能不开的权限就不开</span></td>
              <td class="altbg2" valign="middle">
              	<? popedom()?>
              </td>
            </tr>
            <? if(admin_core::isSuper()){?>
            <tr>
              <td width="40%" class="altbg1" ><b style="color:#FF0000">超级权限:</b><br />
                <span class="smalltxt">超级权限设定，原则上只能是系统管理员与老板才能开这个权限</span></td>
              <td class="altbg2" valign="middle">
              	<div style="display:block; width:120px; height:20px; float:left;">
                    <label for="keyField_superMaster" style="color:#FF0000">
                        <input type="checkbox" id="keyField_superMaster" name="superMaster" value="1" class="checkbox" <?=$rs['superMaster']?'checked="checked"':''?>/>超级权限
                    </label>
                </div>
              </td>
            </tr>
				<? if(!$rs['superMaster']){?>
                <tr>
                  <td width="40%" class="altbg1" ><b style="color:#FF0000">所属上级:</b><br />
                    <span class="smalltxt">所属上级，可以管理本帐号上级帐号</span></td>
                  <td class="altbg2"><input type="text" size="50" require="1" name="higherUp" value="<?=$rs['higherUp']?>" ></td>
                </tr>
                <? }?>
            <? }?>
            <tr>
              <td width="40%" class="altbg1" >
                <b>添加备注:</b><br />
                <span class="smalltxt">备注，记录一些其它信。<br />如：为什么加这个帐号？这个帐号是给谁用，谁要求添加的，等等。</span>
              </td>
              <td class="altbg2">
              	<textarea  rows="6" name="content" id="content" cols="50"><?=$rs['content']?></textarea>
              </td>
            </tr>
          </tbody>
        </table>
        <br />
        <center>
          <?=$Pid?>
          <input type="hidden"  name="Pact" value="<?=$Pact?>">
          <input class="button" type="submit" value="提 交">
        </center>
      </form></td>
  </tr>
</table>
<? }else{
	$where = array();
	if(!admin_core::isSuper())
	{
		$where['where'] = " where `{$admin['table']['field']['higherUp']}` = :{$admin['table']['field']['higherUp']} ";
		$where['bind'][$admin['table']['field']['higherUp']] = admin_core::getMaster();
	}
	$sp    = pg::sp($db_admin,$table,$where,$page,$rows);
	$rs    = $db_admin->dataArray("select * from {$table} {$where['where']} limit {$sp['begin']},{$rows}",$where['bind']);
	//echo $db_admin->getsql();
?>
<style>
td{border:#B4A9CD 1px solid}
</style>
<table width="100%" border="0" cellpadding="2" cellspacing="6">
  <tr>
    <td>
        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
         
          <tr class="header">
            <td colspan="8">管理人员 - 列表</td>
          </tr>
          <tbody>
            <tr class="_data_">
              <td><b>帐号</b></td>
              <td><b>姓名</b></td>
              <td><b>地区部门</b></td>
              <td><b>联系方式</b></td>
              <td><b>权限</b></td>
              <? if(admin_core::isSuper()){?>
              <td><b>超级</b></td>
              <? }?>
              <td><b>备注</b></td>
              <td><b>操作</b></td>
            </tr>
            <? 
			function popedom($master)
			{
				$popedom 	= admin_core::getPopedomGroup($master);
				foreach($popedom as $k=>$v){?>
                <div style="border:#333333 dotted 1px; margin:2px 0 2px; padding:3px;">
                	<b><?=$v['name']?></b>
                    <? foreach($v["sub"] as $kk=>$vv){?>
                    <div style="display:block; width:100px; height:15px; float:left;">
                		<?=$vv['name']?>
                    </div>                    
                    <? }?>
                    <div style="clear:both;"></div>  
                </div>      
               <? }
			}
			foreach($rs as $v)
			{
			?>
            <tr class="_data_">
              <td><?=$v['id']?><b># <?=$v['master']?></b></td>
              <td><b><?=$v['nickname']?></b><br />
                  <i><?=$v['name']?></i></td>
              <td><?=$v['area']?><br />
                <?=$v['branch']?></td>
              <td><?=$v['tel']?><br /><?=$v['mobile']?><br /><?=$v['email']?></td>
              <td>
              	<? popedom($v['master']);?>
              </td>
              <? if(admin_core::isSuper()){?>
              <td><input type="checkbox" style="border:none;" <?=$v['superMaster']?'checked="checked"':'disabled="disabled"'?>/></td>
              <? }?>
              <td><?=$v['content']?>
              <? if(admin_core::isSuper() && $v['higherUp']){ ?>
                  <br />
                  <span style="color:#FF9966;">所属:<b><?=$v['higherUp']?></b></span>
              <? }?>
              </td>
              <td><a href="?Pact=del&id=<?=$v['id']?>" onclick="return oAdmin.msgYn()">删除</a> <a href="?act=modify&id=<?=$v['id']?>">修改</a></td>
            </tr>
            <? }?>
            <tr>
            <td colspan="8"><div class="pages"><em>&nbsp;<?=$sp['str']?>&nbsp;</em><?=implode('',$sp['np']);?></div></td>
          </tr>
          </tbody>
          
        </table>
        </td>
  </tr>
</table>
<? }?>
<br />
<br />
<? include("s_tpl.copyright.php"); ?>
</body>
</html>
